package com.puboot.module.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.module.model.LkCategory;
import com.puboot.module.model.LkComic;
import com.puboot.module.service.LkCategoryService;
import com.puboot.module.service.LkComicService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Locale;

/**
 * (RCateComic)表控制层
 *
 * @author Leonidax
 * @since 2024-10-11 17:00:04
 */
@Controller
@AllArgsConstructor
public class IndexController {

    private final LkCategoryService categoryService;
    private final LkComicService comicService;

    private String initCacheModel(Model model){
        //获取当前语言
        Locale locale = LocaleContextHolder.getLocale();
        String language = locale.getLanguage();
        //分类列表
        List<LkCategory> cates = categoryService.selectCategories();
        model.addAttribute("cates",cates);
        model.addAttribute("lang",language);
        return language;
    }

    @GetMapping("/")
    public String index(Model model){
        String language = initCacheModel(model);
        List<LkComic> random1 = comicService.random(1,9, language);
        List<LkComic> random2 = comicService.random(2,9, language);
        List<LkComic> random3 = comicService.random(3,9, language);
        model.addAttribute("random1",random1);
        model.addAttribute("random2",random2);
        model.addAttribute("random3",random3);
        return "index/index";
    }
    @GetMapping("/search")
    public String search(Model model){
        String language = initCacheModel(model);
        List<LkComic> random4 = comicService.random(4,9, language);
        model.addAttribute("random",random4);
        return "index/search";
    }
    @GetMapping("/search/{word}")
    public String searchResult(Model model, @PathVariable String word){
        String language = initCacheModel(model);
        List<LkComic> list = comicService.search(word,language);
        model.addAttribute("list",list);
        model.addAttribute("word",word);
        return "index/search-result";
    }

    @GetMapping("/rank")
    public String rank(Model model){
        String language = initCacheModel(model);
        IPage<LkComic> page = comicService.rank(1, language);
        List<LkComic> rank = page.getRecords();
        model.addAttribute("rank",rank);
        return "index/rank";
    }

    @GetMapping("/cate")
    public String cates(Model model){
        String language = initCacheModel(model);
        model.addAttribute("cateId",0);
        IPage<LkComic> page = comicService.all(1, language);
        List<LkComic> list = page.getRecords();
        model.addAttribute("list",list);
        return "index/cate";
    }
    @GetMapping("/cate/{cate}")
    public String cate(Model model, @PathVariable Integer cate){
        String language = initCacheModel(model);
        model.addAttribute("cateId",cate);
        IPage<LkComic> page = comicService.cate(cate,1, language);
        List<LkComic> list = page.getRecords();
        model.addAttribute("list",list);
        return "index/cate";
    }


}

